package com.maomao.db;

import java.sql.*;

public class DbConnection {
		
		private Connection conn = null;
		private	ResultSet rs = null;
		
		
		public Connection getConn() throws ClassNotFoundException, SQLException
		{
			String driver = "com.mysql.jdbc.Driver";

			 String url = "jdbc:mysql://127.0.0.1:3306/test";

			 String user = "root";

			 String password = "123456";

			 Class.forName(driver);

			 Connection conn = DriverManager.getConnection(url, user, password);
			 
			 if(!conn.isClosed())

				 System.out.println("Succeeded connecting to the Database!");
			return conn;
		}
		
		public ResultSet excuteQuery(String sql) throws ClassNotFoundException, SQLException
		{
			this.conn = getConn();
				
			Statement statement = conn.createStatement();
				
			this.rs = statement.executeQuery(sql);
			
			return this.rs;
		}
		
		public void update(String sql) throws ClassNotFoundException, SQLException
		{
			this.conn = getConn();
			
			Statement statement = conn.createStatement();
			
			statement.executeUpdate(sql);
			
		}
}
